package com.shopping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.common.Result;
import com.shopping.entity.Order;
import com.shopping.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/admin/orders")
public class AdminOrderController {

    @Resource
    private OrderService orderService;

    /**
     * 获取订单列表（分页）
     */
    @GetMapping
    public Result<Page<Order>> getOrderList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String orderNumber,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Order.OrderStatus status) {

        Page<Order> orderPage = orderService.getOrderListForAdmin(page, size, orderNumber, userId, status);
        return Result.success(orderPage);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{id}")
    public Result<Order> getOrderDetail(@PathVariable Long id) {
        Order order = orderService.getOrderDetail(id);
        return order != null ? Result.success(order) : Result.error("订单不存在");
    }

    /**
     * 更新订单地址
     */
    @PutMapping("/{id}/address")
    public Result<Boolean> updateOrderAddress(@PathVariable Long id, @RequestBody Order orderUpdate) {
        boolean success = orderService.updateOrderAddress(id, orderUpdate.getAddress());
        return success ? Result.success(true) : Result.error("更新订单地址失败");
    }

    /**
     * 更新订单状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateOrderStatus(@PathVariable Long id, @RequestBody Order orderUpdate) {
        boolean success = orderService.updateOrderStatus(id, orderUpdate.getOrderStatus());
        return success ? Result.success(true) : Result.error("更新订单状态失败");
    }

    /**
     * 发货
     */
    @PutMapping("/{id}/ship")
    public Result<Boolean> shipOrder(@PathVariable Long id, @RequestBody Order orderUpdate) {
        boolean success = orderService.shipOrder(id, orderUpdate.getShippingMethod());
        return success ? Result.success(true) : Result.error("发货失败");
    }

    /**
     * 取消订单
     */
    @PutMapping("/{id}/cancel")
    public Result<Boolean> cancelOrder(@PathVariable Long id) {
        boolean success = orderService.cancelOrder(id);
        return success ? Result.success(true) : Result.error("取消订单失败");
    }

    /**
     * 获取最新订单（用于仪表板）
     */
    @GetMapping("/latest")
    public Result<List<Order>> getLatestOrders(@RequestParam(defaultValue = "10") Integer limit) {
        List<Order> orders = orderService.getLatestOrders(limit);
        return Result.success(orders);
    }

    /**
     * 获取订单统计信息
     */
    @GetMapping("/statistics")
    public Result<Object> getOrderStatistics() {
        Object statistics = orderService.getOrderStatistics();
        return Result.success(statistics);
    }
}